package com.qlu.syq.controller.home;

import com.qlu.syq.entity.Account;
import com.qlu.syq.entity.BookOrder;
import com.qlu.syq.entity.RoomType;
import com.qlu.syq.entity.admin.Place;
import com.qlu.syq.service.AccountService;
import com.qlu.syq.service.BookOrderService;
import com.qlu.syq.service.HomeBookService;
import com.qlu.syq.service.RoomTypeService;
import com.qlu.syq.service.admin.FloorService;
import com.qlu.syq.service.admin.PlaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: HomeBookController
 * @Description: 前台预定控制器
 * @Author: xx
 * @Date: 2021/5/21 18:33
 **/
@RequestMapping("/home/book")
@Controller
public class HomeBookController {

    @Autowired
    private FloorService floorService;
    @Autowired
    private PlaceService placeService;
    @Autowired
    private RoomTypeService roomTypeService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private BookOrderService bookOrderService;
    @Autowired
    private HomeBookService homeBookService;


    /***
     * @MethodName: matchingFloorByPlaceId
     * @Description: 通过考点id匹配酒店
     * @Author:
     * @Date: 2021/5/21 18:40
     **/
    @RequestMapping(value = "/matchingFloorByPlaceId", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> matchingFloorByPlaceId(Integer placeId) {
        Map<String, Object> ret = new HashMap<>();
        List<Map<String, Object>> maps = homeBookService.matchingFloorByPlaceId(placeId);
        ret.put("rows", maps);
        return ret;
    }

    /***
     * @MethodName: matchingFloorByPlaceId
     * @Description: 通过考点id匹配酒店
     * @Author:
     * @Date: 2021/5/21 18:45
     **/
    @RequestMapping(value = "/listAllPlace", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> listAllPlace() {
        Map<String, Object> ret = new HashMap<>();
        List<Place> placeList = placeService.findAll();
        ret.put("rows", placeList);
        return ret;
    }

    /**
     * 根据酒店id查询房型
     * @return
     */
    @RequestMapping(value = "/listRoomTypeByFloorId", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> listRoomTypeByFloorId(Long floorId) {
        Map<String, Object> ret = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        map.put("floorId", floorId);
        List<RoomType> roomTypeList = homeBookService.getRoomTypeList(map);
        ret.put("rows", roomTypeList);
        return ret;
    }

    /**
     * 预订
     * @return
     */
    @RequestMapping(value = "/bookOrder", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> bookOrder(BookOrder bookOrder, HttpServletRequest request) throws Exception {
        Map<String, Object> ret = new HashMap<>();

        // 这里没用使用拦截器
        Account account = (Account) request.getSession().getAttribute("account");
        if (account == null) {
            ret.put("type", "error");
            ret.put("msg", "请先登录系统");
            return ret;
        }

        bookOrder.setAccountId(account.getId());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String createTime = sdf.format(new Date());
        bookOrder.setCreateTime(createTime);

        if (homeBookService.bookOrder(bookOrder) <= 0) {
            ret.put("type", "error");
            ret.put("msg", "预定失败");
            return ret;
        }

        ret.put("type", "success");
        ret.put("msg", "");
        return ret;
    }

    @RequestMapping(value = "/vertify", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> vertify(HttpServletRequest request) {
        Map<String, Object> ret = new HashMap<>();
        // 取代拦截器
        Object admin = request.getSession().getAttribute("account");
        if (admin == null) {
            ret.put("type", "_error");
            ret.put("msg", "请先登录系统");
            ret.put("status", false);
            return ret;
        }
        ret.put("type", "success");
        ret.put("msg", "");
        ret.put("status", true);
        return ret;
    }
}
